package com.qmzy.lab05.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qmzy.lab05.entity.Account;
import com.qmzy.lab05.entity.Yqdk;
import com.qmzy.lab05.entity.YqdkCondition;
import com.qmzy.lab05.mapper.NotNullQueryWrapper;
import com.qmzy.lab05.service.IAccountService;
import com.qmzy.lab05.service.IYqdkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjy
 * @since 2021-06-14
 */
@RestController
@RequestMapping("api/yqdk")
public class YqdkController {

    private static List<Yqdk> yqList;

    @Autowired
    private IYqdkService service;

    @Autowired
    private IAccountService accountService;


    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("/list")
    public List<Yqdk> yqdkList(){
        yqList = service.list();
        return yqList;
    }

    @ApiOperation("导出Excel")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("疫情打卡表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Yqdk.class).sheet("疫情打卡").doWrite(yqList);
    }


    @PreAuthorize("hasRole('USER')")
    @Transactional
    @PostMapping("/add")
    public void submmit(@RequestBody Yqdk yqdk){
        service.save(yqdk);
        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<Account>().set("clockin",1).eq("id",yqdk.getId());
        accountService.update(updateWrapper);
    }


    @PreAuthorize("hasRole('USER')")
    @GetMapping("/get")
    public Yqdk getYqdk(@RequestParam("id") Integer id){
//        System.out.println("进入getYqdk");
        Yqdk yqdk = service.getOneByIdDate(id);
        if(yqdk == null){
            yqdk = new Yqdk();
            yqdk.setFinish(0);
        }else if(yqdk.getFull_date().equals(LocalDate.now().toString())){
            yqdk.setFinish(1);
        }else {
            yqdk.setFinish(0);
        }
        yqdk.setId(id);
        yqdk.setFull_date(LocalDate.now().toString());
        return yqdk;
    }


    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/search")
    public List<Yqdk> searchList(@RequestBody YqdkCondition condition){
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("status",condition.getStatus());
        hashMap.put("campus",condition.getCampus());
        QueryWrapper<Yqdk> queryWrapper = new NotNullQueryWrapper<Yqdk>().allEq(hashMap,false)
                .eq("id",condition.getId())
                .eq("high_risk_areas",condition.getHigh_risk_areas())
                .ge("full_date",condition.getStart_date())
                .le("full_date",condition.getEnd_date())
                .ge("temperature",condition.getMin_temperature())
                .le("temperature",condition.getMax_temperature());
        yqList = service.list(queryWrapper);
        return yqList;
    }
}
